﻿//Copyright (C) Riwut Libinuko, 2010 - Ideas For Free
//http://blog.libinuko.com

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharePointZip.Controller;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.SharePoint;
using SharePointZip.Base;

namespace SharePointZip.Tools
{
    /// <summary>
    /// Implementation of <see cref="SharpZipLibCompressorBase&lt;TSource&gt;"/> to compress <see cref="Microsoft.SharePoint.SPListItemCollection"/>.
    /// </summary>
    public class SPListItemCollectionCompressor : SharpZipLibCompressorBase<SPListItemCollection>
    {
        /// <summary>
        /// Create compressed <see cref="Microsoft.SharePoint.SPListItemCollection"/> using <see cref="ZipOutputStream"/>.
        /// </summary>
        /// <param name="zipEngine">Active compression engine</param>
        /// <param name="objSrc">SPListItemCollection to be compressed</param>
        public override void CreateCompressedStream(ZipOutputStream zipEngine, SPListItemCollection objSrc)
        {
            var spFileCompressor = ServiceLocator.Instance.GetService<ICompress<ZipOutputStream, SPFile>>();
            var spFolderCompressor = ServiceLocator.Instance.GetService<ICompress<ZipOutputStream, SPFolder>>();

            foreach (SPListItem item in objSrc)
            {
                if (item.FileSystemObjectType == SPFileSystemObjectType.File)
                {
                    spFileCompressor.CreateCompressedStream(zipEngine, item.File);
                }
                else if (item.FileSystemObjectType == SPFileSystemObjectType.Folder)
                {
                    spFolderCompressor.CreateCompressedStream(zipEngine, item.Folder);
                }
            }
        }
   
    }

   

}
